package dao

import (
	"fmt"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// SystemSmtpLogAttachmentOption SystemSmtpLogAttachment 选项
type SystemSmtpLogAttachmentOption option

// SystemSmtpLogAttachmentOptionWithLock 选项：加锁查询
func SystemSmtpLogAttachmentOptionWithLock() SystemSmtpLogAttachmentOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Clauses(clause.Locking{Strength: SelectForUpdate}), nil
	}
}

// SystemSmtpLogAttachmentOptionWithOrderDefault 选项：默认排序
func SystemSmtpLogAttachmentOptionWithOrderDefault() SystemSmtpLogAttachmentOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Order(fmt.Sprintf("%s.create_time DESC", (&model.SystemSmtpLogAttachment{}).TableName())), nil
	}
}

// SystemSmtpLogAttachmentOptionWithId 选项：根据id查询
func SystemSmtpLogAttachmentOptionWithId(id ...string) SystemSmtpLogAttachmentOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		if 0 == len(id) {
			return nil, errors.ErrorRecordNotFound("IID_AO.SSLAOWI_ID.330357")
		}

		return session.Where(fmt.Sprintf("%s.id IN ?", (&model.SystemSmtpLogAttachment{}).TableName()), id), nil
	}
}

func SystemSmtpLogAttachmentOptionWithSmtpLogId(smtpLogId ...string) SystemSmtpLogAttachmentOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		if 0 == len(smtpLogId) {
			return nil, errors.ErrorRecordNotFound("IID_AO.SSLAOWSLI_ID.431740")
		}

		return session.Where(fmt.Sprintf("%s.smtp_log_id IN ?", (&model.SystemSmtpLogAttachment{}).TableName()), smtpLogId), nil
	}
}
